package development;

import state.DrawListener;
import state.GameAlert;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.Sprite;
import state.component.BottomBar;
import state.component.Event;
import state.component.EventListener;
import state.component.GameMenu;
import state.component.GridMenu;
import state.component.MultiTab;
import state.font.FontManager;
import state.font.ImageFont;
import state.util.GameColor;
import state.util.GameStringUtils;
import state.util.Hashtable;
import state.util.ImageUtil;
import state.util.TwoDimensionString;
import state.util.array.ObjectArray;
import state.util.array.StringArray;
import development.app.AppBussiness;
import development.group.FrmGroupMenu;
import development.socket.SocketClientUtil;

public class FrmChooseApp extends GameForm implements EventListener{
  public StringArray APP_NAME;
  public StringArray APP_ID;
  public TwoDimensionString APP_TOOLTIP;
  public StringArray APP_ICON;
  
  private int HOZ_DISTANCE;
  private int VER_DISTANCE;
  private int DX;
  private int DY;
  private int NUMBER_OF_ITEM_PER_ROW;
  
  private ObjectArray itemsImages;
  private GridMenu gridMenu;
  private Sprite userOnlineSprite;
  private ImageFont text8;
  private GameImage onlineImage;
  
  private static GameForm singleton;
  
  public static GameForm instance() {
    if (singleton == null) {
      singleton = new FrmChooseApp();
    }
    return singleton;
  }
  
  public FrmChooseApp() {
    initAppList();
  }
  
  public void initAppList() {
    APP_NAME = new StringArray(new String[] {   "Game Online",             "Chat",          "Xổ số",         "Bóng đá",        "Thời tiết",        "Kết bạn",        "18+",            "Cửa hàng",        "Tin tức",        "Mạng XH",        "Sự kiện HOT",    "Nạp xu" }, 12);
    APP_ID = new StringArray(new String[] {     "GO",                      "chat",          "xoso",          "bongda",         "thoitiet",         "ketban",         "18+",            "cuahang",         "tintuc",         "mangxh",         "sukienhot",      "napxu" }, 12);
    APP_ICON = new StringArray(new String[]   {  "1.Game-online.png",      "2.Chat.png",    "3.Xo-so.png",   "4.Bong-da.png",  "5.Thoi-tiet.png",  "6.Ket-ban.png",  "7.Cam-nang.png", "8.Cua-hang.png",  "9.Tin-tuc.png",  "10.Mang-XH.png", "11.Su-kien.png", "12.Nap-xu.png"}, 12);
    
    APP_TOOLTIP = new TwoDimensionString(1, 12);
    APP_TOOLTIP.setColumn(0, new StringArray(new String[] {"Game Online",  "Chat",           "Xổ số",        "Bóng đá",         "Thời tiết",        "Kết bạn",        "18+",            "Cửa hàng",         "Tin tức",       "Mạng xã hội",    "Sự kiện HOT",    "Nạp xu"}, 12));
    
    int deleteSuccessCount = 0;
    int index = 0;
f1: while (index < APP_ID.len()) {
      for (int j = 0; j < Global.appToReject.len(); j++) {
        if (APP_ID.get(index).equalsIgnoreCase(Global.appToReject.get(j).trim())) {
          GameStringUtils.deleteElementAtStringArray(APP_NAME, index);
          GameStringUtils.deleteElementAtStringArray(APP_ID, index);
          GameStringUtils.deleteElementAtTwoDimensionStringArray(APP_TOOLTIP, index);
          GameStringUtils.deleteElementAtStringArray(APP_ICON, index);
          deleteSuccessCount++;
          continue f1;
        }
      }
      index++;
    }
    APP_NAME = GameStringUtils.subArrayString(APP_NAME, 0, APP_NAME.len() - deleteSuccessCount);
    APP_ID = GameStringUtils.subArrayString(APP_ID, 0, APP_ID.len() - deleteSuccessCount);
    APP_TOOLTIP = GameStringUtils.subTwoDimensionStringArray(APP_TOOLTIP, 0, APP_TOOLTIP.getWidth() - deleteSuccessCount);
    APP_ICON = GameStringUtils.subArrayString(APP_ICON, 0, APP_ICON.len() - deleteSuccessCount);
  }

  public void init(Hashtable hashtable) {
    itemsImages = ObjectArray.create(APP_ID.len());
    if (Constants.GAME_VERSION_INFO.equals("all")) {
      itemsImages.set(0, ImageUtil.getImage(Constants.MENU_IMAGE_CATEGORY, APP_ICON.get(0)));
    } else {
      int index = Global.getGameIndexById(Constants.GAME_VERSION_INFO);
      itemsImages.set(0, ImageUtil.getImage(Constants.GAME_ICON_CATEGORY, Constants.GAME_ICON.get(index)));
      APP_NAME.set(0, Constants.GAME_NAME.get(index));
      APP_TOOLTIP.set(0, 0, Constants.GAME_TOOLTIP.get(0, index));
    }
    
    for (int i = 1; i < itemsImages.len(); i++) {
      itemsImages.set(i, ImageUtil.getImage(Constants.MENU_IMAGE_CATEGORY, APP_ICON.get(i)));
    }
    
    if (Constants.IS_240x320_SCREEN) {
      HOZ_DISTANCE = 70;
      VER_DISTANCE = 60;
      DX = 50;
      DY = 60;
      NUMBER_OF_ITEM_PER_ROW = 3;
    } else {
      HOZ_DISTANCE = 70;
      VER_DISTANCE = 60;
      DX = 50;
      DY = 60;
      NUMBER_OF_ITEM_PER_ROW = 4;
    }
    
    // Create grid menu
    gridMenu = new GridMenu(_manager, itemsImages, APP_NAME, NUMBER_OF_ITEM_PER_ROW, DX, DY, HOZ_DISTANCE, VER_DISTANCE, this);
    gridMenu.setToolTip(APP_TOOLTIP);
    gridMenu.setForceGroundColor(GameColor.WHITE_CODE);
    gridMenu.setFocusFgColor(GameColor.YELLOW_CODE);
    gridMenu.setEnableScroll(false);
    
    // Display number of user online
    text8 = FontManager.getFont(FontManager.FONT_SIZE_8);
    onlineImage = ImageUtil.getImage(Constants.NEW_IMAGE_CATEGORY, "icon_online.png");
    userOnlineSprite = new Sprite(null, _manager, Constants.SCREEN_WIDTH - 80, 20, Constants.TOP_LEFT_ANCHOR);
    userOnlineSprite.setSize(70, 20);
    userOnlineSprite.setDrawListener(new DrawListener() {
      public void paint(Sprite source, Graphics g) {
        if (Global.numberOfUserOnline > -1) {
          String txt = "Online: " + Global.numberOfUserOnline;
          g.drawImage(onlineImage, Constants.SCREEN_WIDTH - text8.stringWidth(txt) - 23, 29, Constants.VCENTER_LEFT_ANCHOR);
          text8.setBold(true);
          text8.drawString2(g, txt, GameColor.YELLOW_CODE, Constants.SCREEN_WIDTH - text8.stringWidth(txt) - 10, 27, Constants.VCENTER_LEFT_ANCHOR);
          text8.setBold(false);
        }
      }
    });
  }
  
  public GridMenu getGridMenu() {
    return gridMenu;
  }

  public void draw(Graphics g) {
    DrawUtils.drawBackground(g);
  }

  public void onActionPerform(Event event) {
    if (MultiTab.OPEN_TAB_EVENT.equals(event.getAction())) {
      DrawUtils.getBottomBar(FrmGroupMenu.instance().getManager()).setRightLabel("");
      return;
    }
    
    if (event.getSource() == gridMenu) {
      chooseMenu(gridMenu.getCurrentSelectedIndex());
      return;
    }
  }

  public void alertEventPerform(int alertType, int eventType, int alertId) {
    if (alertId == 99 && alertType == GameAlert.YES_NO_TYPE && eventType == GameAlert.YES_BUTTON) { // Thoát ra
      SocketClientUtil.signOut();
      GameGlobal.nextState(FrmLogin.instance(), null);
    } else if (alertId == 98 && alertType == GameAlert.YES_NO_TYPE && eventType == GameAlert.YES_BUTTON) { // Đến diễn đàn
      Global.systemCanvas.getMidlet().platformRequest("http://www.winplay.vn/wap");
    }
  }
  
  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.FIRE:
      BottomBar bottomBar = DrawUtils.getBottomBar(FrmGroupMenu.instance().getManager());
      if (bottomBar != null) {
        GameMenu leftMenu = bottomBar.getLeftMenu();
        if (leftMenu != null && !leftMenu.isShowing()) {
          chooseMenu(gridMenu.getCurrentSelectedIndex());
        }
      }
      break;
    }
  }
  
  private void chooseMenu(int choosedIndex) {
    if (choosedIndex == 0) {
      ((FrmGroupMenu) FrmGroupMenu.instance()).getMultiTab().nextTab();
      return;
    }
    GameForm appForm = AppBussiness.getAppForm(APP_ID.get(choosedIndex));
    if (appForm != null) {
      GameGlobal.nextState(appForm, null);
    }
  }

  public void destroy() {
    itemsImages = null;
    gridMenu.detroy();
    gridMenu = null;
    singleton = null;
    onlineImage = null;
  }
}
